package com.web.graduationpro.manager.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.web.graduationpro.dao.service.ManagerService;
import com.web.graduationpro.model.Manager;
import com.web.graduationpro.util.MD5;

@Controller
@RequestMapping("/manager/*")
public class ManagerIndexController {
	
	@Resource(name="managerServiceImpl")
	private ManagerService managerServiceImpl;
	
	private MD5 md5 = new MD5();
	
	@RequestMapping(value = "/index", method = RequestMethod.GET)  
    public String index() {  
        return "/manager/index";
    }  
	
	@RequestMapping(value = "/toLogin", method = RequestMethod.GET)  
    public String toLogin() {  
        return "/manager/login";
    }  
	
	@RequestMapping(value = "/checkUser", method = RequestMethod.POST)  
    public void checkUser(HttpServletRequest request,HttpServletResponse reponse) {
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		reponse.setContentType("text/html;charset=UTF-8");
		
		String result = "null";
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		String md5Pwd = "";
		if(password!=null && !"".equals(password.trim())){
			md5Pwd = md5.md5s(password);
		}
		
		String[] cses = {username,md5Pwd};
		List<Manager> managers = managerServiceImpl.findByPage(" FROM Manager m where m.userName=? AND m.userPwd=? ",cses, 0, 1);
		if(managers!=null && managers.size()!=0){
			result = "nonNull";
		}
		try {
			PrintWriter writer = reponse.getWriter();
			writer.write(result);
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }  
	
	@RequestMapping(value = "/userLogin", method = RequestMethod.POST)  
    public String userLogin(HttpServletRequest request) {  
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		String md5Pwd = "";
		if(password!=null && !"".equals(password.trim())){
			md5Pwd = md5.md5s(password);
		}
		
		String[] cses = {username,md5Pwd};
		List<Manager> urls = managerServiceImpl.findByPage(" FROM Manager m where m.userName=? AND m.userPwd=? ",cses, 0, 1);
		if(urls!=null && urls.size()!=0){
			request.getSession().setAttribute("loginedUser", username);
			request.getSession().setAttribute("logUserId", urls.get(0).getUserId());
			return index();
		}
		return toLogin();
    }  
	
	@RequestMapping(value = "/logout", method = RequestMethod.GET)  
    public String logout(HttpServletRequest request) {  
		request.getSession().removeAttribute("loginedUser");
        return toLogin();
    } 
}
